const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    activeIndex: 0,
    page: 0,
    coupon:[],
  },
  //生命周期函数--监听页面加载
  onShow:function(){
    app.loginState((rel)=>{
      if(rel){
        this.getApi();
      }else{
        app.wxAlert('你还没有登录',()=>{
          wx.navigateBack({
            delta:1
          })
        })
      }
    },false)
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1500);
    this.setData({
      page: 0,
      loading: true,
      coupon: []
    });
    this.getApi();
  },
  //下拉
  onReachBottom: function () {
    this.setData({
      loading: true,
    });
    this.getApi();
  },
  /**
   * 我的出售API
   */
  getApi() {
    if (this.data.loading) {
      var param = {
        page:this.data.page + 1,
        type:this.data.activeIndex,
      }
      app.api().Get("api/v1/ais/couponuser/index",param,(result)=>{
        if (result.code == 200) {
          var coupon = this.data.coupon;
          for (let i in result.data) {
            coupon.push(result.data[i]);
          }
          this.setData({
            coupon: coupon,
            page: param.page,
          });
        }
        this.setData({
          loading: false,
        });
      })
    }
  },
  //点击Tab切换
  onChange(e) {
    this.setData({page: 0,loading: true,coupon: []});
    this.data.activeIndex = e.detail.index;
    this.getApi();
  },
  //查看优惠券规则
  reCoupon:function(e){
    wx.navigateTo({
      url:e.currentTarget.dataset.url,
      fail: (res) => {
        app.wxAlert('未找到页面')
      }
    })
  }
})